package com.creating.mict.dao;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.creating.mict.domain.Column;



/** 
 * @ClassName: ColumnDao 
 * @Description: 系统栏目Dao
 * @author xuelei
 * @date 2012-3-9 下午7:53:55 
 *  
 */
public class ColumnDao extends HibernateDaoSupport {

private static final Log log = LogFactory.getLog(ColumnDao.class);
	
	
	/**
	 * 
	* @Title: getAllUserReportByLoginName 
	* @Description: 获取当前用户所在角色的栏目
	* @param @param loginName
	* @param @param fflag
	* @param @return 
	* @return List<ReportInfo>    返回类型 
	* @throws
	 */
	public List<Column> getAllColumnByRoleId(String roleId) {
		String sql = "";
		sql = " select a.fbh, a.fname, a.fleaf, a.fflag, a.flevel, a.fparent, a.fdesc, a.fopdate, a.ftype "
			+ " from tds_column a, tds_role c, tds_role_column e " 
			+ " where a.fbh=e.fcolumn and c.fid=e.frole "
			+ " and c.fid='" + roleId + "' "
			+ " order by a.flevel asc, a.fbh asc "; 
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<Column> ri_list = new ArrayList<Column>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			Column ri = new Column();
			for (int j = 0; j < obj.length; j++) {
//				System.out.println("i=" + i + "==j=" + j + "=============" + obj[j]);
				ri.setFbh("" + obj[0]);
				ri.setFname("" + obj[1]);
				ri.setFleaf("" + obj[2]);
				ri.setFflag("" + obj[3]);
				ri.setFlevel("" + obj[4]);
				ri.setFparent("" + obj[5]);
				ri.setFdesc("" + obj[6]);
				ri.setFtype("" + obj[7]);
			}
			ri_list.add(ri);
		}
		return ri_list;
	}
	
	public List<Column> getUserColumnList(String loginName) {
		String sql = "";
		sql = " select a.fbh, a.fname, a.fleaf, a.fflag, a.flevel, a.fparent, a.fdesc, a.fopdate, a.ftype "
			+ " from tds_column a, tds_user b, tds_role c, tds_user_role d, tds_role_column e" 
			+ " where b.floginname=d.fuser and d.frole=c.fid and a.fbh=e.fcolumn and c.fid=e.frole "
			+ " and b.floginname='" + loginName + "' "
			+ " order by a.flevel asc, a.fbh asc "; 
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<Column> ri_list = new ArrayList<Column>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			Column ri = new Column();
			for (int j = 0; j < obj.length; j++) {
				ri.setFbh("" + obj[0]);
				ri.setFname("" + obj[1]);
				ri.setFleaf("" + obj[2]);
				ri.setFflag("" + obj[3]);
				ri.setFlevel("" + obj[4]);
				ri.setFparent("" + obj[5]);
				ri.setFdesc("" + obj[6]);
				ri.setFtype("" + obj[7]);
			}
			ri_list.add(ri);
		}
		return ri_list;
	}
	
	
	
	
	/**
	 * 
	* @Title: getAllReportInfoList 
	* @Description: 获取所有栏目
	* @param @param fflag
	* @param @return 
	* @return List<ReportInfo>    返回类型 
	* @throws
	 */
	public List<Column> getAllColumnList() {
		String sql = "";
		sql = " select a.fbh, a.fname, a.fleaf, a.fflag, a.flevel, a.fparent, a.fdesc, a.fopdate, a.ftype "
			+ " from tds_column a " 
			+ " order by a.flevel asc, a.fbh asc "; 
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<Column> ri_list = new ArrayList<Column>();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			Column ri = new Column();
			for (int j = 0; j < obj.length; j++) {
				ri.setFbh("" + obj[0]);
				ri.setFname("" + obj[1]);
				ri.setFleaf("" + obj[2]);
				ri.setFflag("" + obj[3]);
				ri.setFlevel("" + obj[4]);
				ri.setFparent("" + obj[5]);
				ri.setFdesc("" + obj[6]);
				ri.setFtype("" + obj[7]);
			}
			ri_list.add(ri);
		}
		return ri_list;
	}
	
	
	
	/**
	 * 
	* @Title: saveReportInfo 
	* @Description: 保存报表或图表信息
	* @param @param ri
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean saveColumn(Column ri) {
		try {
			getHibernateTemplate().saveOrUpdate(ri);
			getHibernateTemplate().flush();
			log.info("保存信息成功!");
			return true;
		} catch (DataAccessException e) {
			log.error("保存信息失败!");
			return false;
		}
	}
	
	
	/**
	 * 
	* @Title: getReportInfoByFbh 
	* @Description: 根据编号获取报表或图表对象
	* @param @param fbh
	* @param @return 
	* @return ReportInfo    返回类型 
	* @throws
	 */
	public Column getColumnByFbh(String fbh) {
		try {
			String sql = " select select a.fbh, a.fname, a.fleaf, a.fflag, a.flevel, a.fparent, a.fdesc, a.fopdate, a.ftype "
		               + " from tds_column r where r.fbh='" + fbh + "'";
			Query query = this.getSession().createSQLQuery(sql);
			List list = query.list();
			
			if (list != null && list.size() > 0) {
				Object[] obj = (Object[]) list.get(0);
				Column ri = new Column();
				ri.setFbh("" + obj[0]);
				ri.setFname("" + obj[1]);
				ri.setFleaf("" + obj[2]);
				ri.setFflag("" + obj[3]);
				ri.setFlevel("" + obj[4]);
				ri.setFparent("" + obj[5]);
				ri.setFdesc("" + obj[6]);
				ri.setFtype("" + obj[7]);
				return ri;
			} else {
				return null;
			}
		} catch (RuntimeException re) {
			log.error("获取报表或图表对象失败！", re);
			throw re;
		}
	}
	
	/**
	 * 
	* @Title: deleteReportInfo 
	* @Description: 删除报表（或报表类别）信息
	* @param @param fbh
	* @param @param ftype
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean deleteColumn(String fbh, String ftype) {
		try {
			String sql = "delete tds_column " 
					   + " where fbh='" + fbh + "'";
			
			if(ftype.equals("2")) {
				sql += " or fparent='" + fbh + "'";
			}

			Query query = this.getSession().createSQLQuery(sql);
			int num = query.executeUpdate();
			if (num > 0) {
				return true;
			} else {
				return true;
			}

		} catch (RuntimeException re) {
			log.error("删除报表信息失败！", re);
			throw re;
		}
	}
	
	
}
